//https://leetcode.cn/problems/longest-substring-without-repeating-characters/?envType=study-plan-v2&envId=top-100-liked
//思想：1.滑动窗口

#include <string>
#include <iostream>
#include <map>
using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string str) {
        if (str.size()<2) return str.size();
        std::map<char,int>m;
        int l=0,r=1;
        m[str[0]]=0;
        int max_len=0;
        while(l<=r && (size_t)r<str.size()){
            if (m.find(str[r]) == m.end()){
                m[str[r]] = r;
            }else {
                int same_index=m[str[r]];
                while(l<=same_index){
                    m.erase(str[l++]);
                }
                m[str[r]] = r;
            }
            max_len = std::max(max_len,r-l+1);
            r++;
        }
        return max_len;
    }
};